package org.example.myportfolio.config.configuration;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * redis配置类
 */
@Configuration
public class RedisConfiguration {


    /**
     * 告诉IOC容器，创建一个RedisTemplate对象
     * 通过redisTemplate来操作redis 访问数据库进行操作
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate(); // 创建一个模板对象
        //设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);

        //设置key序列化器: 将key变成序列化的形式
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //设置value序列化器
        redisTemplate.setValueSerializer(RedisSerializer.json());

        //对pageinfo对象进行序列化
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());

        //PageInfo<WorkPO> 序列化和反序列化


        return redisTemplate;
    }
























}
